﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Windows.Storage.Streams;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace IndustrialSmokeCalc
{
    public partial class Menu : PhoneApplicationPage
    {
        public Menu()
        {
            InitializeComponent();

            DataSingleton.Instance.ChartHistogram = new int[5][];
            Dispatcher.BeginInvoke(delegate
            {
                DataSingleton.Instance.ChartHistogram[0] = OpacityMeasurement.GetCumulativeHistogram(OpacityMeasurement.ConvertToWbmp("20.png"));
                DataSingleton.Instance.ChartHistogram[1] = OpacityMeasurement.GetCumulativeHistogram(OpacityMeasurement.ConvertToWbmp("40.png"));
                DataSingleton.Instance.ChartHistogram[2] = OpacityMeasurement.GetCumulativeHistogram(OpacityMeasurement.ConvertToWbmp("60.png"));
                DataSingleton.Instance.ChartHistogram[3] = OpacityMeasurement.GetCumulativeHistogram(OpacityMeasurement.ConvertToWbmp("80.png"));
                DataSingleton.Instance.ChartHistogram[4] = OpacityMeasurement.GetCumulativeHistogram(OpacityMeasurement.ConvertToWbmp("100.png"));
            });
        }

        private void AppBarAbout_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/About.xaml", UriKind.Relative));
        }

        private void AppBarSetting_Click(object sender, EventArgs e)
        {
            MessageBox.Show(DataSingleton.Instance.UserId);
            //NavigationService.Navigate(new Uri("/Setting.xaml", UriKind.Relative));
        }

        private void AppBarLogout_Click(object sender, EventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Apakah anda yakin untuk logout?", "", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

                if (settings.Contains("UserId"))
                {
                    settings.Remove("UserId");
                }
                NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
            }
        }

        private void BorderPengukuran_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            DataSingleton.Instance.MapAccessor = "measurement";
            NavigationService.Navigate(new Uri("/InputMeasurement.xaml", UriKind.Relative));
        }

        private void BorderDraft_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Draft.xaml", UriKind.Relative));
        }

        private void BorderHistory_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            DataSingleton.Instance.MapAccessor = "history";
            NavigationService.Navigate(new Uri("/ChooseCompany.xaml", UriKind.Relative));
        }

        private void BorderBenchmark_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Benchmark.xaml", UriKind.Relative));
        }

        private void BorderHelp_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            DataSingleton.Instance.MapAccessor = "measurement";
            NavigationService.Navigate(new Uri("/ChooseCompany.xaml", UriKind.Relative));
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            while (NavigationService.BackStack.Any())
            {
                NavigationService.RemoveBackEntry();
            }
        }
    }
}